<?
class ManageImageUploadForm extends Form
{
    function ManageImageUploadForm()
    {
        Form::Form('ManageImageUploadForm');

		$this->link_css('style/ManageImage/manageimage.css');
        $this->link_css("style/paging.css");

		$this->link_js('javascript/fileuploader.js');

    }
    function draw()
    {
        global $display;

		$display->add("WEB_DIR",WEB_DIR);
        $display->add("IMAGE_PATH",IMAGE_PATH);

		if (User::is_login()){
            $display->add("isLogin", true);
            if(!User::have_permit(array("hotel_add", "room_add"))) {
                $display->output('NotPermission');
                return;
            }
		} else {
            add("isGuest", $_COOKIE["GuestID"]);
            Url::refresh();
            return;
		}

        $this->beginForm(false,'post',false,Url::build_current());

        $images = glob(ROOT_PATH . 'images/user/' . User::id() . '/'  . "{*.jpg,*.gif,*.png}", GLOB_BRACE);
        $listFileNames = array();
        foreach ($images as $key => $aImg){
            $imgExplode = explode('/', $aImg);
            $listFileNames[$key]['name'] = end($imgExplode);
            $listFileNames[$key]['url'] = WEB_ROOT . 'images/user/' . User::id() . '/' . $listFileNames[$key]['name'];
            $listFileNames[$key]['id'] = str_replace('.','_',$listFileNames[$key]['name']);
        }
        $display->add("listFileNames", $listFileNames);

        $display->output('ManageImageUpload');
        $this->endForm();
    }

	function on_submit()
    {
        global $display;
        if(User::is_login() && User::have_permit(array("hotel_add", "room_add"))){

            $images = Url::get('imagesAdd');

            $listImages = array();

            $keyImg = 0;
            
            foreach ($images as $nameUploaded => $named) {

                $fileUploaded = explode('_',$nameUploaded);
                $fileName   = 	String::safe_title($named);
                if (!$fileName){
                    $named = $fileUploaded[0];
                    $fileName = $fileUploaded[0];
                } else {
                    mb_internal_encoding("UTF-8");
                    $named = mb_strtoupper(mb_substr($named,0,1)) . mb_substr($named,1);
                }
                
                $data['user_id'] = User::id();
                $data['time_c'] = TIME_NOW;
                $data['img_name'] = $named;
                $data['type'] = 2;

                $up_folder = Upload::folderUpload($data['user_id']);
                $local_file 		= 	ROOT_PATH . 'images/user/' . User::id() . '/' .$fileUploaded[0] . '.' . $fileUploaded[1];

                $data['img_url'] = 'images/user/'. User::id() . '/' .$fileUploaded[0] . '.' . $fileUploaded[1];
                $data['img_server'] = 0;

                $idImage = DB::insert('images', $data);
                $listImages[$keyImg]['id'] = $idImage;
                $listImages[$keyImg]['name'] = $named;
                $listImages[$keyImg]['url'] = WEB_ROOT . '/' . $data['img_url'];

                if (Upload::storage_check_dir($up_folder, true, IMAGE_SERVER_NO))
                {
                    $destination_file 	= 	$up_folder . $idImage . '/' . $fileName . '.' . $fileUploaded[1];
                    if(Upload::storage_put_file($destination_file, $local_file, IMAGE_SERVER_NO))
                    {
                        unlink($local_file);
                        $dataUpdate['img_url'] = $destination_file;
                        $dataUpdate['img_server'] = 1;
                        DB::update_id('images', $dataUpdate, $idImage);
                        $listImages[$keyImg]['url'] = IMAGE_PATH . '/' . $dataUpdate['img_url'];
                    }
                }
                ++$keyImg;
            }
            if ($listImages) {
                $listImages = json_encode($listImages);
                $display->add('listImages', $listImages);
            }
        }
    }
}
?>